package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.UserService;
import com.xuanzheng.company.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register/manager")
public class ManagerRegisterServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        
        // 获取请求参数
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        int age = Integer.parseInt(req.getParameter("age"));
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
        String idCard = req.getParameter("idCard");
        String companyName = req.getParameter("companyName");
        String companyAddress = req.getParameter("companyAddress");
        String companyPhone = req.getParameter("companyPhone");
        String companyCreditCode = req.getParameter("companyCreditCode");
        
        // 创建用户对象
        User user = new User();
        user.setName(name);
        user.setGender(gender);
        user.setAge(age);
        user.setPhone(phone);
        user.setPassword(password);
        user.setEmail(email);
        user.setAddress(address);
        user.setIdCard(idCard);
        user.setCompanyName(companyName);
        user.setCompanyAddress(companyAddress);
        user.setCompanyPhone(companyPhone);
        user.setCompanyCreditCode(companyCreditCode);
        
        // 响应结果
        Map<String, Object> result = new HashMap<>();
        
        // 检查手机号是否已存在
        if (userService.isPhoneExist(phone)) {
            result.put("success", false);
            result.put("message", "该手机号已被注册");
        } else {
            // 调用注册服务
            String userId = userService.registerCompanyManager(user);
            
            if (userId != null) {
                result.put("success", true);
                result.put("message", "注册成功，您的ID是：" + userId);
                result.put("userId", userId);
            } else {
                result.put("success", false);
                result.put("message", "注册失败，请重试");
            }
        }
        
        // 输出JSON响应
        PrintWriter out = resp.getWriter();
        out.write(gson.toJson(result));
        out.flush();
        out.close();
    }
} 